package com.huike.controller;

import com.huike.service.IInformationService;
import com.huike.service.IPolicyService;
import com.huike.utils.FileUploadUtil;
import com.huike.utils.JsonUtils;
import com.huike.utils.ResultMessage;
import com.huike.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BaseController {

	@Autowired
	IInformationService informationService;

	@Autowired
	IPolicyService policyService;

	/**
	 * ��ҳ��
	 * 
	 * @return
	 */
	@RequestMapping("")
	public String index() {
		return "index";
	}

	/**
	 * ��ҳ
	 * 
	 * @return
	 */
	@RequestMapping("/mainPage")
	public String mainPage() {
		return "mainPage";
	}

	/**
	 * ���˽���ҳ
	 * 
	 * @return
	 */
	@RequestMapping("/unionIntroduce")
	public String unionIntroduce() {
		return "unionIntroduce";
	}

	@RequestMapping("/certificate")
	public String certificate() {
		return "certificate";
	}

	/**
	 * ���˶�̬ҳ
	 * 
	 * @return
	 */
	@RequestMapping("/unionDynamic")
	public String unionDynamic(HttpServletRequest request) {
		int total = informationService.count();
		request.setAttribute("total",total);
		return "unionDynamic";
	}

	/**
	 * ������ҳ
	 * 
	 * @return
	 */
	@RequestMapping("/newPolicy")
	public String newPolicy(HttpServletRequest request) {
		int total = policyService.count();
		request.setAttribute("total",total);
		return "newPolicy";
	}

	/**
	 * 首页点击最新资讯跳转后展示详细数据的页面
	 * 
	 * @return
	 */
	@RequestMapping("/informationDetail")
	public String informationDetail() {
		return "informationDetail";
	}

	/**
	 * 导航栏《最新政策》中点击政策后跳转到展示详细数据的页面
	 * 
	 * @return
	 */
	@RequestMapping("/newPolicyDetail")
	public String newPolicyDetail() {
		return "newPolicyDetail";
	}

	/**
	 * 文件上传方法
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping("/fileUpload")
	@ResponseBody
	public ResultMessage fileUpload(@RequestParam("file") MultipartFile file) {
		String resUrl = null;
		try {
			String oldName = file.getOriginalFilename();
			InputStream inputStream = file.getInputStream();
			String newName = UUIDUtil.getUUId() + oldName.substring(oldName.lastIndexOf("."));
			resUrl = FileUploadUtil.upload(newName, inputStream);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultMessage.fail();
		}
		if (resUrl == null) {
			return ResultMessage.fail();
		}
		return new ResultMessage(true, resUrl);
	}

	/**
	 * 富文本编辑器批量图片上传方法
	 * 
	 * @param file
	 * @param response
	 */
	@RequestMapping("/ImageBatchUpload")
	@ResponseBody
	public void ImageBatchUpload(@RequestParam("files") MultipartFile file, HttpServletResponse response) {
		//List<String> list = new ArrayList<>();
		String resUrl = null;
		try {
			String oldName = file.getOriginalFilename();
			InputStream inputStream = file.getInputStream();
			String newName = UUIDUtil.getUUId() + oldName.substring(oldName.lastIndexOf("."));
			resUrl = FileUploadUtil.upload(newName, inputStream);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> errorMap = new HashMap<>();
			errorMap.put("errno", -1);
			JsonUtils.jsonArray(errorMap, response);
			return;
		}
		//list.add(resUrl);
		Map<String, Object> map = new HashMap<>();
		//map.put("errno", 0);
		//map.put("data", list);
		map.put("url", resUrl);
		JsonUtils.jsonArray(map, response);
	}

}
